package com.hitangjun.rel;

import com.alibaba.fastjson.JSONObject;

import static com.hitangjun.rel.MessagePotting.*;
import static com.hitangjun.utils.MessageUtils.*;
import static com.hitangjun.utils.RxConvertHelper.*;

/**
 * @create 2021-02-02 20:57
 * PC控制类
 **/
public class ControlPC {
    //数据大小+数据+crc最大的长度
    public static final byte MAX_DATASIZE_DATA_CRC_LENGTH = 56;
    //数据大小最大的长度
    public static final byte MAX_DATA_LENGTH = 52;

    public static final byte HEAD = -86;
    public static final byte ADDRESS_SERVER_TO_EQUIPMENT = 2;
    public static final byte ADDRESS_EQUIPMENT_TO_PC = 16;
    public static final byte ADDRESS_SERVER_TO_PC = 18;
    public static final byte ADDRESS_EQUIPMENT_TO_SERVEZR = 32;

    public  static final byte RESPONSE_OK=-1;
    public  static final byte RESPONSE_ERROR=-2;
    public  static final byte RESPONSE_WAIT=-3;

    /**
     *
     * 解析服务器发来的应答报文
     * 1.判断头部是否正确
     * 2.判断地址的方向
     * 3.判断是否需要续传
     * 其中的数据是一次性发送，需要判断是否续传
     */
    public static void pcAnalysisServerResponse(String jsonstr){
        //将string字符串转成JSON对象
        JSONObject json = JSONObject.parseObject(jsonstr);

        byte head = Byte.parseByte(json.getString("head"));
        byte address = Byte.parseByte(json.getString("address"));
        byte[] command = (json.getString("command")).getBytes();
        byte[] data = (json.getString("data")).getBytes();
        //对JSON进行判断
        if(HEAD != head){
            System.out.println("头部错误");
        }else{
            if(ADDRESS_SERVER_TO_PC == address){
                System.out.println("PC自己处理");
            }else if(ADDRESS_SERVER_TO_EQUIPMENT == address){
                //需要续传
                if(MAX_DATASIZE_DATA_CRC_LENGTH < data.length){
                    //调用续传的方法
                    sendSequel(command,data);
                }
                else{
                    System.out.println("服务器到设备，PC不做处理，转发到设备,不需要续传");
                    //调用方法封装一个报文
                    byte[] requestMessageBytes = pottingPcToEquipmentMessage(command,data);
                    //调用方法，把这个报文发出去
                    //sendRequestToEquipment(requestMessageBytes);
                }
            }
        }
    }

    /**
     * 解析设备发来的应答报文
     *三种应答报文分别处理
     */
    public static void pcAnalysisEquipmentResponse(byte[] equipmentResponseMessage){
        //或得头部
        int headBytes = equipmentResponseMessage[0];
        //获得地址
        int address = equipmentResponseMessage[1];
        //获得响应命令 从报文中截取命令
        byte command1 = equipmentResponseMessage[2];
        byte command2 = equipmentResponseMessage[3];
        if (HEAD != headBytes) {
            System.out.println("设备响应报文头部出错。");
        }else{
            if (ADDRESS_EQUIPMENT_TO_PC != address){
                System.out.println("设备响应报文地址信息出错。");
            }else{
                if (RESPONSE_OK == command1){
                    System.out.println("设备成功接收。");
                }else if(RESPONSE_ERROR == command1){
                    System.out.println("请求设备失败。");
                }else if(RESPONSE_WAIT == command1){
                    System.out.println("请求设备超时");
                }else{
                    System.out.println("设备响应报文命令错误");
                }
            }

        }

    }
}
